package com.obas.component.tree {
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.component.tree.treeclass.ErrorNode;
   import com.obas.component.tree.treeclass.LoadingNode;
   import com.obas.component.tree.treeclass.Node;
   import com.obas.constants.TestingLibConstants;
   import com.obas.constants.StringConstants;
   import com.obas.event.ItestOperationEvent;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.business.SystemFunction;
   import com.obas.model.business.SystemProject;
   import com.obas.model.business.SystemScenario;
   import com.obas.model.business.SystemUsecase;
   import com.obas.model.business.TestcaseScript;
   import com.obas.view.menu.CaseMenu;
   import com.obas.view.menu.FunctionMenu;
   import com.obas.view.menu.ProjectEmptyMenu;
   import com.obas.view.menu.ProjectMenu;
   import com.obas.view.menu.ScenarioMenu;
   import com.obas.view.menu.ScenarioTestReporterMenu;
   import com.ease.component.controls.Menu;

   import flash.events.Event;

   import mx.collections.ArrayCollection;
   import mx.core.Container;
   import mx.events.MenuEvent;

   [Event(name="{com.obas.event.ItestOperationEvent.REQUEST_ID}", type="com.obas.event.ItestOperationEvent")]
   public class TestReporterTree extends LoadingTree {
      public function TestReporterTree() {

      }


      override protected function buildMenu(container:Container):void {
         var scenarioMenu:Menu = new ScenarioTestReporterMenu();
         container.addChild(scenarioMenu);
         _menuMap[TestingLibConstants.ITEMTYPE_SCENARIO] = scenarioMenu;
      }

      //点击展开节点，获取节点下的node
      override protected function callRemoteService(node:Node):void {
         var entity:* = node.entity;
         if (entity is SystemProject) {
            dispatchEvent(new ItestOperationEvent([entity.id, node], ItestOperationEvent.GET_SCENARIO));
         }
         else if (entity is SystemScenario) {
            dispatchEvent(new ItestOperationEvent([entity.id, node], ItestOperationEvent.GET_CASE));
         }
         else if (entity is SystemUsecase) {
            dispatchEvent(new ItestOperationEvent([entity.id, node], ItestOperationEvent.GET_FUNCTION));
         }
         else if (entity is SystemFunction) {
            dispatchEvent(new ItestOperationEvent([entity.id, node], ItestOperationEvent.GET_SCRIPT));
         }
      }

      [ResponseHandler("{com.obas.event.ItestOperationEvent.RETURN_ID}")]
      public function onItestOperationEvent(event:CommandCompletionEvent):void {
         var loadingTreeEvent:ItestOperationEvent = event.request as ItestOperationEvent;
         var requestNode:Node = loadingTreeEvent.spec[1];

         if (event.error) {
            const errorMsg:String = ResourceUtil.getErrorString(StringConstants.EXPLORER_ERROR, event.error);
            addErrorStat(requestNode, errorMsg);
            return ;
         }

         var result:ArrayCollection = event.result as ArrayCollection;
         removeNodeStat(requestNode);
         var childNode:Node = null;
         for each (var childEntity:Object in result) {
            childNode = nodeFactory.createNode(childEntity);
            childNode.parentNode = requestNode;
            childNode.type = getNodeType(childEntity);
            if (childNode.type == TestingLibConstants.ITEMTYPE_SCRIPT) {
               childNode.children = null;
            }
            requestNode.children.addItem(childNode);
         }

         requestNode.initialized = true;
         expandItem(requestNode, false);
         expandItem(requestNode, true);
      }

      public function getNodeType(item:*):String {
         if (item is SystemProject) {
            return TestingLibConstants.ITEMTYPE_PROJECT;
         }
         else if (item is SystemScenario) {
            return TestingLibConstants.ITEMTYPE_SCENARIO;
         }
         else if (item is SystemUsecase) {
            return TestingLibConstants.ITEMTYPE_CASE;
         }
         else if (item is SystemFunction) {
            return TestingLibConstants.ITEMTYPE_FUNCTION;
         }
         else if (item is TestcaseScript) {
            return TestingLibConstants.ITEMTYPE_SCRIPT;
         }
         return "";
      }
   }
}